package com.sinosoft.perfect.system._resource.service;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sinosoft.perfect.system._.po.TResourceSystem;
import com.sinosoft.perfect.system._.po.TResourceSystemImg;
import com.sinosoft.perfect.system._.service.ISortNumberService;
import com.sinosoft.perfect.system._.util.Producer;
import com.sinosoft.perfect.system._.vo.SortNumber;
import com.sinosoft.perfect.system._resource.dao.IResourceDAO;

public class ResourceServiceImpl implements IResourceService {

	private IResourceDAO resourceDAO;

	private ISortNumberService sortNumberService;

	public void setSortNumberService(ISortNumberService sortNumberService) {
		this.sortNumberService = sortNumberService;
	}

	public void setResourceDAO(IResourceDAO resourceDAO) {
		this.resourceDAO = resourceDAO;
	}

	public String insertTResource(TResourceSystem tresourcesystem) throws SQLException {
		int sortNum = this.selectTResourceMAXSortNum(tresourcesystem) + 1;
		String id = Producer.generateIdOfTen();
		tresourcesystem.setResourceId(id);
		if (tresourcesystem.getSuperResourceId() == null || "".equals(tresourcesystem.getSuperResourceId()))
			tresourcesystem.setSuperResourceId("0");
		tresourcesystem.setResourceSortNumber(new Long(sortNum));
		tresourcesystem.setCreateUserId("1");
		tresourcesystem.setResourceType("1");
		tresourcesystem.setStatus("1");
		resourceDAO.insertTResource(tresourcesystem);
		return id;
	}

	public List<TResourceSystem> selectTResource(TResourceSystem tresourcesystem) throws SQLException {
		return resourceDAO.selectTResource(tresourcesystem);
	}

	public int hasTResourceName(TResourceSystem tresourcesystem) throws SQLException {
		int result = 0;
		Object obj = resourceDAO.selectTResourceByName(tresourcesystem);
		if (obj != null)
			result = 1;
		return result;
	}

	public String[] selectTResourceAndChildIds(String resourceId) throws SQLException {
		List<TResourceSystem> tResources = resourceDAO.selectTResourceAndChildren(resourceId);
		int resourcesLength = tResources.size();
		String[] resourceIds = new String[resourcesLength];
		for (int i = 0; i < resourcesLength; i++) {
			resourceIds[i] = tResources.get(i).getResourceId();
		}
		return resourceIds;
	}

	public Long selectTResourceSortNum(TResourceSystem tresourcesystem) throws SQLException {
		Long sortNum = null;
		Object obj = resourceDAO.selectTResourceSortNum(tresourcesystem);
		if (obj != null)
			sortNum = Long.parseLong(obj.toString());
		return sortNum;
	}

	public Integer selectTResourceMAXSortNum(TResourceSystem tresourcesystem) throws SQLException {
		Integer sortNum = 0;
		Object numObj = resourceDAO.selectTResourceMAXSortNum(tresourcesystem);
		if (numObj != null)
			sortNum = Integer.parseInt(numObj.toString());
		return sortNum;
	}

	public Integer selectTResourceSortNumMin(TResourceSystem tresourcesystem) throws SQLException {
		Integer sortNum = 0;
		Object obj = resourceDAO.selectTResourceSortNumMin(tresourcesystem);
		if (obj != null)
			sortNum = Integer.parseInt(obj.toString());
		return sortNum;
	}

	public Long selectTResourceSortNumByName(TResourceSystem tresourcesystem) throws SQLException {
		Long sortNum = null;
		Object obj = resourceDAO.selectTResourceSortNumByName(tresourcesystem);
		if (obj != null)
			sortNum = Long.parseLong(obj.toString());
		return sortNum;
	}

	public void updateTResourceByPK(TResourceSystem tresourcesystem) throws SQLException {
		resourceDAO.updateTResourceByPK(tresourcesystem);
	}

	public String[] updateTResourceForStatusByPK(TResourceSystem tresourcesystem) throws SQLException {
		List<TResourceSystem> tRecourses = resourceDAO.selectTResourceAndChildren(tresourcesystem);
		String[] resourceIds = null;
		if (tRecourses != null && tRecourses.size() != 0) {
			int resourcesLength = tRecourses.size();
			resourceIds = new String[resourcesLength];
			for (int i = 0; i < resourcesLength; i++) {
				resourceIds[i] = tRecourses.get(i).getResourceId();
			}
		}
		return resourceIds;
	}

	public void deleteTResourceByPK(String resourceId) throws SQLException {
		resourceDAO.deleteTRoleResourcesByResourceId(resourceId);
		resourceDAO.deleteTResourcesByResourceId(resourceId);
	}

	public void deleteTResources(String[] resourceIds) throws SQLException {
		for (String resourceId : resourceIds) {
			resourceDAO.deleteTRoleResourcesByResourceId(resourceId);
			resourceDAO.deleteTResourcesByResourceId(resourceId);
		}
	}

	public boolean sortResource(long newSortNumber, String resourceId) throws SQLException {
		SortNumber sortNumber = new SortNumber(newSortNumber, resourceId, "T_RESOURCE_SYSTEM", "RESOURCE_ID", "SUPER_RESOURCE_ID", "RESOURCE_SORT_NUMBER");
		sortNumberService.sortNumber(sortNumber);
		return false;
	}

	@Override
	public List<TResourceSystem> selectTResourceAndParents(String resourceId)
			throws SQLException {
		return resourceDAO.selectTResourceAndParents(resourceId);
	}

	@Override
	public List<String> getResourceImgsName(String fpath) {
		File fd = new File(fpath);
		List<String> l = null;
		if(!fd.isDirectory()){
			return l;
		}
		for(int i=0;i<fd.listFiles().length;i++){
			if(l == null) l = new ArrayList<String>();
			l.add(fd.listFiles()[i].getName());
			
		}
		return l;
	}

	@Override
	public void insertTResourceImg(TResourceSystemImg tresourceSystemImg)
			throws SQLException {
		tresourceSystemImg.setId(Producer.generateIdOfTen());
		tresourceSystemImg.setImgType("1");
		resourceDAO.insertTResourceImg(tresourceSystemImg);
		
	}

	@Override
	public void updateTResourceImg(TResourceSystemImg tresourceSystemImg)
			throws SQLException {
		resourceDAO.updateTResourceImg(tresourceSystemImg);
		
	}

	@Override
	public void deleteTResourceImg(TResourceSystemImg tresourceSystemImg)
			throws SQLException {
		resourceDAO.deleteTResourceImg(tresourceSystemImg);
		
	}

	@Override
	public List<TResourceSystemImg> selectTResourceImg(
			TResourceSystemImg tresourceSystemImg) throws SQLException {
		return resourceDAO.selectTResourceImg(tresourceSystemImg);
	}
}
